// 引入 express    npm install express
let express = require('express');
let router = express.Router();
let query = require('./Database');
// 引入 fs
let fs = require('fs');
// 引入 body-parser     npm install body-parser
let bodyParser = require("body-parser");
// 引模块:npm install mysql
const mysql = require('mysql');
//
var path = require('path');
/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('ConfirmOrder', {});
});
//返回页面
router.post('/redraw',async (req, res) => {
    let useridString = req.body.useridString;
    let useridArr = useridString.split(',');
    let Shop = await  query('select distinct goods.shopid,shop.shopname from goods join shop where goods.shopid = shop.id and goods.state =2');
    let Product = [],sum = [];
    for(let i=0;i<useridArr.length;i++){
        Product.push(await query('select * from goods where id = ' + useridArr[i] + ''));
        // sum.push(parseFloat((await query('select cartprice from goods where id = ' + useridArr[i] + ''))[0].cartprice)*parseFloat((await query('select cartcount from goods where id = ' + useridArr[i] + ''))[0].cartcount));
    };
    let data = {
        code:100,
        Product:Product,
        Shop:Shop
    };
    res.send(data);
});
//收货地址
router.post('/Address',async function (req, res) {
    let userid = req.body.userid;
    let address = await query('select * from address where userid =' + userid + '');
    let data = [{data:100},address];
    res.send(data);
});
//删除地址
router.post('/Delete',async function (req, res) {
    let id = req.body.id;
    await query('update address set type=0 where id =' + id + '');
    res.send({data:100});
});
//设置默认地址
router.post('/SetDefault',async function (req, res) {
    let id = req.body.id;
    let userid = req.body.userid;
    await query('update address set type=1 where type =2 and userid ='+userid+' ');
    await query('update address set type=2 where id =' + id + '');
    res.send({data:100});
});
//确认设置默认地址
router.post('/OrderSetDefault',async function (req, res) {
    let id = req.body.id;
    let userid = req.body.userid;
    let address = req.body;
    await query('update address set type=1 where type =2 and userid ='+userid+' ');
    // await query('update address set type=2 where id =' + id + '');
    await query('update address set address = '+address.address+',username='+address.username+',beizhu='+address.beizhu+',phone='+address.phone+',type=2 where id =' + id + '');
    res.send({data:100});
});
//新增地址
router.post('/NewAddress',async function (req, res) {
    let address = req.body;
    await query('insert into address(userid,address,username,phone,beizhu) values("'+ address.userid +'","'+ address.address +'","'+ address.username +'","'+ address.phone +'","'+ address.beizhu +'")');

    res.send({data:100});
});
//点击 提交订单
router.post('/confimorder',function (req, res) {
    console.log(req.body);
    res.send({data:100});
});

module.exports = router;